<?php

namespace common\models\shop;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%shop_store}}".
 *
 * @property int $id
 * @property string $name 名称
 * @property string $logo logo
 * @property string $description 描述
 * @property string $cover 背景封面
 * @property int $type_id 类型ID
 * @property int $fans 粉丝数
 * @property int $point 评分值
 * @property string $location 所在地区
 * @property string $telephone 客服电话
 * @property int $status
 * @property int $created_at
 * @property int $updated_at
 * @property int $deleted_at
 */
class ShopStore extends \common\db\ActiveRecord {

    /**
     * 店铺状态
     */
    CONST STATUS_DEFAULT = 0;
    CONST STATUS_ACTIVE = 10;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%shop_store}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['name'], 'required'],
            [['type_id', 'fans', 'point', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['name', 'telephone'], 'string', 'max' => 50],
            [['logo', 'description', 'cover', 'location'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '名称',
            'logo' => 'logo',
            'description' => '描述',
            'type_id' => '类型ID',
            'cover' => '封面',
            'fans' => '粉丝数',
            'point' => '评分值',
            'location' => '所在地区',
            'telephone' => '客服电话',
            'status' => 'Status',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'deleted_at' => 'Deleted At',
        ];
    }

    /**
     * 店铺类型
     */
    public function getType() {
        return $this->hasOne(ShopStoreType::className(), ['id' => 'type_id']);
    }

    /**
     * 店铺收藏
     */
    public function getCollected() {
        return $this->hasOne(ShopFavorite::className(), ['store_id' => 'id'])->andWhere([
            'user_id' => Yii::$app->user->id
        ])->exists();
    }

    /**
     * 收藏列表
     */
    public function getCollects() {
        return $this->hasMany(ShopFavorite::className(), ['store_id' => 'id']);
    }

    /**
     * 店铺推荐
     */
    public function getRecommends() {
        return $this->hasMany(ShopRecommend::className(), ['store_id' => 'id']);
    }

    /**
     * 店铺推荐商品
     */
    public function getSpreadProducts() {
        return $this->hasMany(\common\logics\ShopProductMinLogic::className(), ['id' => 'product_id'])->via('recommends')->limit(3);
    }

    /**
     * 店铺标签
     */
    public function getTag() {
        if (!$this->type) {
            return '';
        }
        return Url::attach($this->type->thumb);
    }

    /**
     * 店铺LOGO
     */
    public function getBrand() {
        return Url::attach($this->logo);
    }

    /**
     * 店铺封面
     */
    public function getCoverUrl() {
        return Url::attach($this->cover);
    }

    /**
     * 店铺封面
     */
    public function getService() {
        return Url::weblink('call');
    }

    /**
     * 店铺封面
     */
    public function getLink() {
        return Url::weblink('shop.store', [
            'id' => $this->id,
            'tag' => 'home',
        ]);
    }

    /**
     * 店铺封面
     */
    public function getLinkGoods() {
        return Url::weblink('shop.store', [
            'id' => $this->id,
            'tag' => 'goods',
        ]);
    }

    /**
     * @inheritdoc
     */
    public function fields() {
        return [
            'id',
            'name',
            'brand',
            'description',
            'type',
            'cover' => 'coverUrl',
            'fans',
            'point',
            'location',
            'created_at',
            'telephone',
            'service',
            'link',
            'linkGoods',
            'spreadProducts',
        ];
    }

}
